const app = getApp();
Page({
    data: {
        config: app.config,

        idCard: '',
        name: '',
        btnShow:false,
        query: { id: '' },
        point:{
           
        }
    },
    onLoad(query) {
        my.setNavigationBar({
          reset: true
        });
        let obj = { id: '' };
        obj.id = query.id;
        console.log(query);
        this.setData({
            query: obj
        });
        this.getuserId();
    },
    
    getuserId() {
        let user = my.getStorageSync({ key: 'user' });
        console.log(user)
        if (user.data) {
            let name = user.data.name ? user.data.name:'';
            let idCard = user.data.idCard ? user.data.idCard:'';
            let btnShow = (name && idCard)? true:false;
            console.log(name, idCard);
            this.setData({ name: name, idCard: idCard,btnShow:btnShow });
        }
        console.log(user)
    },
    bindKeyInput(e) {
        if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(e.detail.value))) {
            this.setData({
                btnShow: false,
            });
            return
        } else {
            this.setData({
                name: e.detail.value.replace(/\s+/, ''),
                btnShow: true,
            });
        }
    },
    bindIdInput(e) {
        let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        let _IDre15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
        if(_IDRe18.test(e.detail.value) || _IDre15.test(e.detail.value)){
            this.setData({
                idCard: e.detail.value.replace(/\s+/, ''),
                btnShow: true,
            });
            console.log("正常")
        }else {
            this.setData({
                btnShow: false,
            });
            console.log("不正常")
        }
    },
    async onsumclick() {
        if (!this.data.name || !this.data.idCard) {
            my.alert({
                content: '请输入正确信息',
            });
        };
        let data = {
            idCard: this.data.idCard,
            name: this.data.name
        };
        const auth = await app.user.authFace(data)
        console.log("authFace",auth);
        const apv = await app.user.startAPVerify(auth.data);
        console.log("apv",apv);
        if(apv.resultStatus!=9000) return;

        // 保存信息到缓存
        let user = my.getStorageSync({ key: 'user' });
        user.data.name = this.data.name;
        user.data.idCard = this.data.idCard;
        my.setStorageSync({key: 'user',data: user.data });

        // 保存到服务器
        const save = await app.user.authSave(data)
        const human = await app.user.humanAuth(this.data.query.id)
        console.log("保存用户信息",save)
        console.log("更改订单状态",human)
        my.reLaunch({ url: "/pages/order/orderList/orderList"});
    }
});
